<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
class Community extends Controller
{
    public function index()
    {
        $aid = input('get.aid');
        $where = [];
        if(!empty($aid)){
            $where['aid'] = $aid;
        }
        $pages = pages('community',$where,input('get.page'));
        $data = Db::table('community')->whereIn('aid',session('aid'))->where($where)->limit($pages['limit'])->order('id desc')->select();
        return view('',['data'  => $data,'pages' => $pages['pages']]);
    }

    public function edit()
    {
        $id = input("get.id")?input("get.id"):0;
        if(request()->isPost()){
            $data = input("post.");
            if($id){
                Db::table("community")->where(['id' => $id])->update($data);
                $data['id'] = $id;
            }else{
                $data['id'] = Db::table("community")->insertGetId($data);
            }
        }else{
            $data = Db::table("community")->where(['id' => $id])->find();
        }
        return view('',['data'  => $data]);
    }

    public function del()
    {
        $id = input("get.id")?input("get.id"):0;
        $page = input("get.page");
        Db::table("community")->whereIn('aid',session('aid'))->delete($id);
        redirect(url('/admin/community').'?page='.$page);
        exit;
    }
}